package com.mak001.lite.multiSkill.subs;

import org.rsbuddy.tabs.Inventory;
import org.rsbuddy.widgets.Bank;

import extras.mak001.AnimWatcher;
import com.mak001.lite.multiSkill.MainLite;
import com.mak001.lite.multiSkill.Methods;
import extras.mak001.Time;
import com.mak001.lite.multiSkill.subScript;
import com.rsbuddy.event.events.MessageEvent;
import com.rsbuddy.script.methods.Npcs;
import com.rsbuddy.script.methods.Players;
import com.rsbuddy.script.methods.Skills;
import com.rsbuddy.script.methods.Walking;
import com.rsbuddy.script.wrappers.Area;
import com.rsbuddy.script.wrappers.Npc;
import com.rsbuddy.script.wrappers.Tile;

public class PowerFisherLite extends subScript {

	Area bankArea;
	Area fishArea;

	private Area stileArea = new Area(new Tile(2845, 3140),
			new Tile(2853, 3146));
	private Area karamja = Methods.karamja;
	private Area karBoat = new Area(new Tile(2951, 3140), new Tile(2960, 3144));

	boolean stile;
	boolean drop;
	int[] gear;
	int[] spot;
	String fishName;
	String interact;

	private boolean switchB;

	private void setSwitch(boolean b) {
		if (switchB != b)
			switchB = b;
	}

	public enum state {
		WALKTOSPOT, WALKTOBANK, FISH, BANK, USESTILES, GOTOMAINLAND, GOTOKARAMJA
	}

	public state getState() {
		if (karamja.contains(Players.getLocal().getLocation())
				&& !karBoat.contains(Players.getLocal().getLocation())) {
			if (Inventory.isFull()) {
				if (bankArea.contains(Players.getLocal().getLocation())) {
					if (stile == false) {
						return state.BANK;
					} else {
						return state.USESTILES;
					}
				} else {
					return state.WALKTOBANK;
				}
			} else {

				if (fishArea.contains(Players.getLocal().getLocation())) {
					return state.FISH;
				} else {
					if (stile == true) {
						if (MainLite.move.getRemaining() <= 10000) {
							return state.GOTOMAINLAND;
						} else {
							return state.WALKTOSPOT;
						}
					} else {
						return state.WALKTOSPOT;
					}
				}
			}
		} else {
			return state.GOTOKARAMJA;
		}
	}

	@Override
	public void execute() {
		if (bankArea == null) {
			if (stile == true) {
				bankArea = stileArea;
			} else {
				bankArea = Methods.getNearestBank(fishArea.getCentralTile());
			}
		}
		switch (getState()) {
		case WALKTOSPOT:
			setSwitch(false);
			Walking.findPath(fishArea.getCentralTile()).traverse();
			break;
		case WALKTOBANK:
			Walking.findPath(bankArea.getCentralTile()).traverse();
			break;
		case BANK:
			setSwitch(true);
			if (Bank.isOpen()) {
				Bank.depositAllExcept(gear);
			} else {
				Bank.open();
			}
			break;
		case USESTILES:
			Npc stiles = Npcs.getNearest(11267);
			if (stiles != null) {
				stiles.interact("exchange");
			}
			break;
		case FISH:
			if (spot() != null && AnimWatcher.isIdle()
					&& !Players.getLocal().isMoving()) {
				if (spot().isOnScreen()) {
					if (spot().interact(interact)) {
						sleep(1000);
					}
				} else {
					Walking.findPath(spot()).traverse();
				}
			}
			break;
		case GOTOMAINLAND:
			if (karamja.contains(Players.getLocal().getLocation())
					|| Methods.portBoat.contains(Players.getLocal()
							.getLocation())) {
				Methods.toMainLand();
			} else {
				setSwitch(true);
			}
			break;
		case GOTOKARAMJA:
			Methods.toKaramja();
			setSwitch(false);
			break;
		}
	}

	private Npc spot() {
		return Npcs.getNearest(spot);
	}

	@Override
	public int[] required() {
		return gear;
	}

	public void setInf(int[] gear, int[] spot, Area fishA, boolean d,
			boolean k, String inter, String s) {
		this.fishArea = fishA;
		this.gear = gear;
		this.spot = spot;
		this.drop = d;
		this.fishName = s;
		this.interact = inter;
		this.stile = k;
	}

	@Override
	public boolean canSwitch() {
		return switchB;
	}

	@Override
	public String getStatus() {
		if (getState() == state.BANK) {
			return "Banking";
		} else if (getState() == state.FISH){
			return "Fishing";
		} else if (getState() == state.GOTOKARAMJA) {
			return "Going to Karamja";
		} else if (getState() == state.GOTOMAINLAND) {
			return "Going to Main Land";
		} else if (getState() == state.USESTILES) {
			return "Using Stiles";
		} else if (getState() == state.WALKTOBANK) {
			return "Walking to bank";
		} else {//Walking to spot
			return "Walking to spot";
		}
	}

	@Override
	public int skill() {
		return Skills.FISHING;
	}

	@Override
	public void messageRecieved(MessageEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String[] paintString() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Time getTime() {
		// TODO Auto-generated method stub
		return null;
	}

}
